package com.spike.servlet;

import com.spike.model.Dept;
import com.spike.service.DeptService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deptUpdate")
public class DeptUpdateServlet extends HttpServlet {    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    req.setCharacterEncoding("utf-8");
    String name = req.getParameter("name");
    String address = req.getParameter("address");
    int id = Integer.parseInt(req.getParameter("id"));
    DeptService deptService = new DeptService();
    Dept dept = new Dept();
    dept.setId(id);
    dept.setName(name);
    dept.setAddress(address);
    try {
        int i = deptService.updateDept(dept);
        if (i > 0) {//修改成功
            req.getRequestDispatcher("/deptList").forward(req, resp);
        }else {//修改失败
            req.setAttribute("dept", dept);
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/deptUpdate.jsp").forward(req, resp);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}
